package com.kehongborui.fliggy.util;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONPObject;
import org.apache.commons.pool2.PooledObjectState;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

import java.io.IOException;

/**
 * @author zhang peng yang ming
 * @CopyRight (C) http://www.csdn.com
 * @brief
 * @email
 * @date 2018-09-06
 */
@Component("")
public class JedisUtil {
    protected final static Logger logger = Logger.getLogger(JedisUtil.class);

    @Autowired
    private JedisPool jedisPool;

//    @Autowired(required = true)
//    public void setJedisPool(JedisPool jedisPool) {
//        JedisUtil.jedisPool = jedisPool;
//    }

    /**
     * 对某个键的值自增
     * @author liboyi
     * @param key 键
     * @param cacheSeconds 超时时间，0为不超时
     * @return
     */
    public long setIncr(String key, int cacheSeconds) {
        long result = 0;
        Jedis jedis = null;
        try {
            jedis = jedisPool.getResource();
            result =jedis.incr(key);
            if (cacheSeconds != 0) {
                jedis.expire(key, cacheSeconds);
            }
            logger.debug("set "+ key + " = " + result);
        } catch (Exception e) {
            logger.warn("set "+ key + " = " + result);
        } finally {
            // 把资源换回给连接池
            if (jedis != null) {
                jedis.close();
            }
        }
        return result;
    }

    /**
     * 设置缓存
     * @param key 键
     * @param value 值
     * @param cacheSeconds 超时时间，0为不超时
     * @return
     */
    public boolean set(String key, String value, int cacheSeconds) {
        boolean result = false;
        Jedis jedis = null;
        try {
            jedis = jedisPool.getResource();
            String ret = jedis.set(key, value);
            if (cacheSeconds != 0) {
                jedis.expire(key, cacheSeconds);
            }

            result = "OK".equals(ret);

            logger.debug("set "+ key + " = " + value);
        } catch (Exception e) {
            logger.warn("set "+ key + " = " + value);
        } finally {
            if (jedis != null) {
                jedis.close();
            }        }
        return result;
    }

    /**
     * 设置永久缓存
     * @param key
     * @param value
     * @return
     */
    public boolean set(String key, String value) {
        return set(key, value, 0);
    }

    /**
     * 直接存对象,不超时
     * @param key
     * @param value
     * @return
     */
    public boolean setObject(String key, Object value) {
        return set(key, JSON.toJSONString(value));
    }

    /**
     * 直接存对象,有超时时间
     * @param key
     * @param value
     * @param cacheSeconds 存储时间见
     * @return
     */
    public boolean setObject(String key, Object value, int cacheSeconds) {
        return set(key, JSON.toJSONString(value), cacheSeconds);
    }

    /**
     * 获取字符串类型缓存
     * @param key 键
     * @return 值
     */
    public String get(String key) {
        String value = null;
        Jedis jedis = null;

        try {
            jedis = jedisPool.getResource();
            if (jedis.exists(key)) {
                value = jedis.get(key);
                value = !CommonUtils.isEmpty(value) && !"nil".equalsIgnoreCase(value) ? value : null;
                logger.debug("get "+ key + " = " + value);
            }
        } catch (Exception e) {
            logger.warn("get "+ key + " = " + value);
        } finally {
            // 把资源换回给连接池
            if (jedis != null) {
                jedis.close();
            }
        }
        return value;
    }

    /**
     * 从缓存中获取对象
     * @param key
     * @param clazz
     * @param <T>
     * @return
     */
    public <T>T getObject(String key, Class<T> clazz) {
        String ret = get(key);
        return JSON.toJavaObject(JSON.parseObject(ret), clazz);
    }

    /**
     * 向队列中的list放入参数
     * @param key 列表名称
     * @param o
     * @return
     */
    public long lPush(String key, Object o) {
        long ret = 0;
        Jedis jedis = null;

        try {
            jedis = jedisPool.getResource();
            ret = jedis.lpush(key, JSON.toJSONString(o));
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            // 把资源换回给连接池
            if (jedis != null) {
                jedis.close();
            }
        }

        return ret;
    }

    /**
     * 从队列右端获取值
     * @param key
     * @param clazz
     * @param <T>
     * @return
     */
    public <T>T  rPop(String key, Class<T> clazz){
        T ret = null;
        Jedis jedis = null;

        try {
            // 获取jedis 客户端
            jedis = jedisPool.getResource();
            if (jedis.exists(key)) {  // 判断key 是否存在
                String rpop = jedis.rpop(key);
                ret = JSON.toJavaObject(JSON.parseObject(rpop), clazz);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            // 把资源换回给连接池
            if (jedis != null) {
                jedis.close();
            }
        }

        return ret;
    }

    /**
     * 获取队列长度
     * @param key 列表名称
     * @return
     */
    public long len(String key) {
        long ret = 0;
        Jedis jedis = null;

        try {
            jedis = jedisPool.getResource();
            ret = jedis.llen(key);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            // 把资源换回给连接池
            if (jedis != null) {
                jedis.close();
            }
        }

        return ret;
    }
}
